package GDoc::Job::Fetch::US::Exec::Treas::FMS::DTS;

use Moose;

extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://fms.treas.gov/dtsarchive/' );
    $self->_basedir( 'http://fms.treas.gov' );
}

sub run { 
    my $self = shift;
    my %data = %{ $self->data };

    if ( $data{link} ) { 
        $self->_fetch_pdf;
    } elsif ( $data{FY} ) { 
        $self->_fetch_index;
    } else { 
        $self->logger->info( "Fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        # the submit buttons are not in a proper form so we have to scrape
        # them out manually. :(
        my $html = $mech->response->content;
        my @inputs = ( $html =~ m{<input type="submit".*?name="FY" value="([\s\d]+)"}g );

        # gat latest year only
        my ( $inp ) = ( sort { $b <=> $a } @inputs )[0];

        $self->logger->info( "Found form input $inp" );
        my $job = GDoc::Job->new( class    => 'Fetch::US::Exec::Treas::FMS::DTS',
                                  queue    => $self->fetch_queue,
                                  data     => { FY => $inp }
                                );

        $job->submit;
    }
}

sub _fetch_index { 
    my $self = shift;
    my %data = %{ $self->data };

    my $fy = $data{FY};
    my $mech = $self->_mech;

    $self->logger->info( "Fetching index page for FY $fy" );
    $mech->post( $self->_baseurl, { FY => $fy } );

    my @pdfs = $mech->find_all_links( url_regex => qr{\.pdf$} );
    foreach my $link( @pdfs ) { 
        my $job = GDoc::Job->new( class      => 'Fetch::US::Exec::Treas::FMS::DTS',
                                  queue      => $self->fetch_queue,
                                  data       => { link => $self->_basedir . $link->url }
                                );

        $job->submit;
    }
}

sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my $link = $data{link};
    $link =~ m{/fy\s(?<fy>\d{4})/(?<year>\d{2})(?<month>\d{2})(?<day>\d{2})\d+\.pdf$};
    my ( $fy, $year, $month, $day ) = @+{ 'fy', 'year', 'month', 'day' };
    $year = $year < 97 
      ? $year + 2000 
      : $year + 1900;    # y2k
    
    unless( $fy and $year and $month and $day ) { 
        die "Can't parse date info from link $link";
    }

    my $date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
    my $ident = 'FMS-DTS-' . $date;
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/treas/fms/' . $filename;

    if ( $self->_check_key( $key ) ) { 
        $self->logger->info( "Already have $key, skipping" );
        return;
    }

    $self->logger->info( "Storing $link --> $key" );

    my $mech = $self->_mech;
    $mech->get( $link );

    $self->store_pdf( key        => $key,
                      data       => $mech->response->content,
                      filename   => $filename,
                      meta       => { description => $date,
                                      identifier  => $ident }
                    );
}


1;
